package ru.ifmo.ipimenov.ewf_svn.standalone.controller;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNCommitClient;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Item;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Model;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class DeployAction implements ActionListener {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(DeployAction.class);

    private JList list;

    public DeployAction(JList list){
        this.list = list;
    }

    public void actionPerformed(ActionEvent e) {
        if (! (list.getModel() instanceof Model)){
            log.error("Please acquire information first from server");
        }
        Model m = (Model)list.getModel();
        Item i = m.getItem(list.getSelectedIndex());
        i.setStatus(Item.DEPLOYED);
        list.updateUI();
        File f = i.getFile();
        if (f == null){
            log.error("File was not processed");
            return;
        }
        importSelected(i);
        log.info(i.getFile() + " was imported into trunk");
    }

    private void importSelected(Item i){
        DAVRepositoryFactory.setup();
        SVNRepositoryFactoryImpl.setup();
        FSRepositoryFactory.setup();
        
        BasicAuthenticationManager iam = new BasicAuthenticationManager("ilya.pimenov", "n7s9m9d3");
        iam.setAuthenticationForced(true);
        SVNCommitClient client = new SVNCommitClient(iam, null);

        client.setIgnoreExternals(true);
        try {
            client.doDelete(new SVNURL[]{updateWithAuth(i.getUrl())}, "Deleting old version of document");
            client.doImport(i.getFile(), updateWithAuth(i.getUrl()), "Deployed new version of document", true, false);
        } catch (SVNException e) {
            log.error("Failed to import to svn server. " + e);
        }
    }

    private SVNURL updateWithAuth(SVNURL url){
        SVNURL u = null;
        try {
            u = SVNURL.create("https", "ilya.pimenov:n7s9m9d3", url.getHost(), 443, url.getPath(), true);
        } catch (SVNException e) {
            log.error("Failed to update uri");
        }
        log.info("Updated url = " + u.toString());
        return u;
    }

}
